package com.example.shop_2212.fg

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.example.base.mvvm.BaseFragment
import com.example.base.net.Const
import com.example.shop_2212.R
import com.example.shop_2212.adapter.VpAdapter
import com.example.shop_2212.databinding.FragmentMineBinding
import com.example.shop_2212.fgviewmodel.MineViewModel
import com.example.shop_2212.minefg.BlankFragment
import com.example.shop_2212.minefg.BlankFragment2
import com.example.shop_2212.minefg.MyTabEntity
import com.example.shop_2212.utils.GlideUtils
import com.flyco.tablayout.listener.CustomTabEntity
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector

class MineFragment : BaseFragment<FragmentMineBinding,MineViewModel>() {
    override fun getLayout(): Int  = R.layout.fragment_mine

    override fun getViewModel(): Class<MineViewModel>  = MineViewModel::class.java
    lateinit var vpAdapter: VpAdapter

    override fun initView() {
        viewModel.userInfo()
        //选项选择器
        binding.sex.setOnClickListener {
            val list = mutableListOf("男","女","不明")
            val optionPicker = OptionPicker(requireActivity())
            optionPicker.setData(list)
            optionPicker.setTitle("请选择性别")
            optionPicker.setOnOptionPickedListener { position, item ->
                binding.sex.text = list[position]
            }
            optionPicker.show()
        }
        //日期选择器
        binding.birth.setOnClickListener {
            val datePicker = DatePicker(requireActivity())
            datePicker.setTitle("请选择出生年月日")
            datePicker.wheelLayout.setRange(DateEntity.target(1900,1,1), DateEntity.today(),
                DateEntity.today())
            datePicker.setOnDatePickedListener { year, month, day ->
                binding.birth.text = "$year-$month-$day"
            }
            datePicker.show()
        }
        //退出登录
        binding.logout.setOnClickListener {
            SPUtils.getInstance().clear()
            ARouter.getInstance().build("/app/LoginCodeActivity").navigation()
        }
        //变更头像
        binding.icon.setOnClickListener {
            PictureSelector.create(this,200).selectPicture()
        }
        val list = arrayListOf<CustomTabEntity>()
        list.add(MyTabEntity("待付款",0,0))
        list.add(MyTabEntity("待发货",0,0))
        //显示菜单和vp
        binding.tab.setTabData(list)
        val fglist = mutableListOf<Fragment>()
        fglist.add(BlankFragment())
        fglist.add(BlankFragment2())
        vpAdapter = VpAdapter(requireActivity(),fglist)
        binding.vp.adapter = vpAdapter
    }

    override fun initData() {
        viewModel.infoSuccess.observe(this){
            if(it.code==Const.RESULT_OK){
                GlideUtils.loadCircleImage(requireActivity(),it.data.avatar,binding.icon)
                binding.nickName.text = it.data.nickname
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==200&&resultCode==Activity.RESULT_OK){
            val pictureBean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            GlideUtils.loadCircleImage(requireActivity(),pictureBean.path,binding.icon)
        }
    }

}